import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import junit.awtui.TestRunner;
import junit.framework.TestSuite;

public class Command {

    private String input = "input.txt", output = "output.txt";
    private final LinkedStack<Integer> stack = new LinkedStack<Integer>();

    public Command() {
        DataInputStream inputStream = null;
        DataOutputStream outputStream = null;
        try {
            inputStream = new DataInputStream(new FileInputStream(input));
            outputStream = new DataOutputStream(new FileOutputStream(output));
            String line;
            while ((line = inputStream.readLine()) != null) {
                try {
                    String result = runCommand(line.split(" "));
                    outputStream.write((result + "\n").getBytes());
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    private String runCommand(String[] args) throws IllegalArgumentException {
        if(args.length == 0) {
            throw new IllegalArgumentException("fail");
        }
        String command = args[0];
        if (command.equals("push")) {
            try {
                stack.push(Integer.valueOf(args[1]));
                return "pushed "+args[1];
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException("push failed");
            }
        } else if (command.equals("pop")) {
            return String.valueOf(stack.pop());
        } else if (command.equals("peek")) {
            return String.valueOf(stack.peek());
        } else if (command.equals("size")) {
            return String.valueOf(stack.size());
        } else if (command.equals("clear")) {
            stack.clear();
            return "stack cleared";
        } else if (command.equals("toString")) {
            return stack.toString();
        } else {
            throw new IllegalArgumentException("no such command");
        }
    }

    public static void main(String[] args) {
        TestRunner runner = new TestRunner();
        TestSuite suite = new TestSuite(Test.class);
        runner.doRun(suite);
        new Command();
    }

    }
